package cn.zkrt.iotms.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtils {
    public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static String formatDateTime(TemporalAccessor temporal) {
        return DATETIME_FORMATTER.format(temporal);
    }

    public static String subtract8Hours(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

        return sdf.format(calendar.getTime());
    }

    public static String subtract8Hours(String strDate) {
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = s.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

        return sdf.format(calendar.getTime());
    }


    public static Date getDate(String time){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        try {
            return format.parse(time);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }


    public static String getDateByInflux(String time){

        String output ="";
        try {
            DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
            LocalDateTime dateTime = LocalDateTime.parse(time, inputFormatter);
            DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            output = dateTime.format(outputFormatter);
        }catch (Exception e){
            DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm");
            LocalDateTime dateTime = LocalDateTime.parse(time, inputFormatter);
            DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            output = dateTime.format(outputFormatter);
        }

        return output;
    }

// * 时间戳人工调试到整点
//     * 负1分钟,正2分钟 之内的都调整到整点
    public static String adjustmentsTimeStamp(String timeStamp, Integer negativeMinute, Integer positiveMinute) {
        if (StringUtils.isBlank(timeStamp)) {
            return timeStamp;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(Long.valueOf(timeStamp));
        Date time = calendar.getTime();
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        if (minute + negativeMinute >= 60) {
            //需要向上取整
            if (hour + 1 < 24) {
                //那么直接加时间
                calendar.add(Calendar.HOUR_OF_DAY, 1);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                long timeInMillis = calendar.getTimeInMillis();
                Date time1 = calendar.getTime();
                String format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time1);
                return String.valueOf(timeInMillis);

            } else {
                //表示hour+1变成了24,那么就变成第二天的凌晨了,日期需要加一天
                calendar.add(Calendar.DAY_OF_YEAR, 1);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                long timeInMillis = calendar.getTimeInMillis();
                Date time1 = calendar.getTime();
                String format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time1);
                return String.valueOf(timeInMillis);
            }
        }
        if ((second != 0 && minute + 1 - positiveMinute <= 0) || (second == 0 && minute - positiveMinute <= 0)) {
            //表示需要向下取整,而小时数本身就是向下取整的
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            long timeInMillis = calendar.getTimeInMillis();
            Date time1 = calendar.getTime();
            String format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time1);
            return String.valueOf(timeInMillis);
        }
        long timeInMillis = calendar.getTimeInMillis();
        Date time1 = calendar.getTime();
        String format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time1);
        return String.valueOf(timeInMillis);
    }

}
